package com.example.lotterysystem.service.activitystatus.impl;

import com.example.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.example.lotterysystem.common.exception.ServiceException;
import com.example.lotterysystem.service.ActivityService;
import com.example.lotterysystem.service.activitystatus.ActivityStatusManager;
import com.example.lotterysystem.service.activitystatus.operater.AbstractActivityOperator;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Component
public class ActivityStatusManagerImpl implements ActivityStatusManager {

    private static final Logger logger = LoggerFactory.getLogger(ActivityStatusManagerImpl.class);

    @Autowired
    private final Map<String, AbstractActivityOperator> opeartorMap = new HashMap<>();

    @Autowired
    private ActivityService activityService;



    @Override
    public void handlerEvent(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //人员 奖品
        if (CollectionUtils.isEmpty(opeartorMap)) {
            logger.warn("opeartorMap 为空!!");
            return;
        }
        //map<String,

        Map<String,AbstractActivityOperator> currMap = new HashMap<>(opeartorMap);

        Boolean update = false;
        //人员,奖品
        update = processConverStatus(convertActivityStatusDTO,currMap,1);


        //活动
        update = processConverStatus(convertActivityStatusDTO,currMap,2) || update;
        if(update){
            activityService.cacheActivity(convertActivityStatusDTO.getActivityId());

        }




    }




    @Override
    public void rollbackHandlerEvent(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //回滚活动
        for (AbstractActivityOperator operator : opeartorMap.values()) {
            operator.convert(convertActivityStatusDTO);
        }
        //缓存更新
        activityService.cacheActivity(convertActivityStatusDTO.getActivityId());
    }




    /**
     * 扭转状态
     * @param convertActivityStatusDTO
     * @param currMap
     * @param sequence
     * @return
     */
    private Boolean processConverStatus(ConvertActivityStatusDTO convertActivityStatusDTO,
                                        Map<String, AbstractActivityOperator> currMap,
                                        int sequence) {
        Boolean update = false;
        //遍历map
        Iterator<Map.Entry<String,AbstractActivityOperator>> iterator = currMap.entrySet().iterator();
        //Operator是否需要转换

        while (iterator.hasNext()) {
            AbstractActivityOperator operator = iterator.next().getValue();
            if (operator.sequence() != sequence || !operator.needConvert(convertActivityStatusDTO)) {
                continue;
            }
            if (!operator.convert(convertActivityStatusDTO)){
                logger.error("{}状态转换失败,",operator.getClass().getName());
                throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_CONVERT_ERROR);
            }

            iterator.remove();
            update = true;
        }


        //需要转换


        //Map删除当前Operator


        //return

        return update;


    }
}
